package com.hzcx.app.simplechat.util.picker;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.user.bean.CityListBean;
import com.hzcx.app.simplechat.util.dp.DpUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class PickerUtil {

    private static OptionsPickerView pvOptions;
    private static List<String> sexList;
    private static TimePickerView pvTime;

    /**
     * 展示性别选择器
     *
     * @param context
     * @param listener
     */
    public static void showSexPicker(Context context, OnOptionsSelectListener listener) {
        sexList = new ArrayList<>();
        sexList.add("保密");
        sexList.add("男");
        sexList.add("女");
        pvOptions = new OptionsPickerBuilder(context, listener).setLayoutRes(R.layout.view_picker, v -> {
            TextView tvCancel = v.findViewById(R.id.tv_cancel);
            TextView tvCommit = v.findViewById(R.id.tv_commit);
            TextView tvTitle = v.findViewById(R.id.tv_title);
            tvCancel.setOnClickListener(view -> pvOptions.dismiss());
            tvCommit.setOnClickListener(view -> {
                pvOptions.returnData();
                pvOptions.dismiss();
            });
            tvTitle.setText("请选择性别");
        })
                .setContentTextSize(DpUtils.dp2px(context, 8))
                .setTextColorCenter(context.getResources().getColor(R.color.color_282F3E))
                .setTextColorOut(context.getResources().getColor(R.color.color_B2B2B2))
                .build();
        pvOptions.setPicker(sexList);
        pvOptions.show();
    }

    /**
     * 获取性别数据
     *
     * @return
     */
    public static List<String> getSexList() {
        return sexList;
    }


    /**
     * 展示日期选择器
     *
     * @param context
     * @param listener
     */
    public static void showDatePicker(Context context, OnTimeSelectListener listener) {
        Calendar startDate = Calendar.getInstance();
        Calendar firstDate = Calendar.getInstance();
        startDate.set(1900, 0, 1);
        firstDate.set(2010, 0, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DATE));
        pvTime = new TimePickerBuilder(context, listener).setDate(firstDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.view_picker_date, v -> {
                    TextView tvCancel = v.findViewById(R.id.tv_cancel);
                    TextView tvCommit = v.findViewById(R.id.tv_commit);
                    TextView tvTitle = v.findViewById(R.id.tv_title);
                    tvCancel.setOnClickListener(view -> pvTime.dismiss());
                    tvCommit.setOnClickListener(view -> {
                        pvTime.returnData();
                        pvTime.dismiss();
                    });
                    tvTitle.setText("请选择生日");
                })
                .setContentTextSize(DpUtils.dp2px(context, 8))
                .setTextColorCenter(context.getResources().getColor(R.color.color_282F3E))
                .setTextColorOut(context.getResources().getColor(R.color.color_B2B2B2))
                .build();
        pvTime.show();
    }

    /**
     * 展示性别选择器
     *
     * @param context
     * @param listener
     */
    public static void showCityPicker(Context context, List<CityListBean> cityData, OnOptionsSelectListener listener) {

        ArrayList<String> options1Items = new ArrayList<>();// 省集合
        ArrayList<ArrayList<String>> options2Items = new ArrayList<>();// 市集合
        ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();// 区集合

        for (int i = 0; i < cityData.size(); i++) {
            options1Items.add(cityData.get(i).getName());
            ArrayList<String> cityList = new ArrayList<>();//该省的城市列表（第二级）
            ArrayList<ArrayList<String>> province_AreaList = new ArrayList<>();//该省的所有地区列表（第三极）
            if (cityData.get(i).getProvince() == null || cityData.get(i).getProvince().size() <= 0) {
                cityList.add("");
                ArrayList<String> city_AreaList = new ArrayList<>();//该城市的所有地区列表
                city_AreaList.add("");
                province_AreaList.add(city_AreaList);
            } else {
                for (int k = 0; k < cityData.get(i).getProvince().size(); k++) {
                    cityList.add(cityData.get(i).getProvince().get(k).getName());
                    ArrayList<String> city_AreaList = new ArrayList<>();//该城市的所有地区列表
                    if (cityData.get(i).getProvince().get(k).getCity() == null || cityData.get(i).getProvince().get(k).getCity().size() <= 0) {
                        city_AreaList.add("");
                    } else {
                        for (int j = 0; j < cityData.get(i).getProvince().get(k).getCity().size(); j++) {
                            city_AreaList.add(cityData.get(i).getProvince().get(k).getCity().get(j).getName());
                        }
                        province_AreaList.add(city_AreaList);
                    }
                }
            }
            options2Items.add(cityList);
            options3Items.add(province_AreaList);

        }


        pvOptions = new OptionsPickerBuilder(context, listener).setLayoutRes(R.layout.view_picker, v -> {
            TextView tvCancel = v.findViewById(R.id.tv_cancel);
            TextView tvCommit = v.findViewById(R.id.tv_commit);
            TextView tvTitle = v.findViewById(R.id.tv_title);
            tvCancel.setOnClickListener(view -> pvOptions.dismiss());
            tvCommit.setOnClickListener(view -> {
                pvOptions.returnData();
                pvOptions.dismiss();
            });
            tvTitle.setText("请选择地区");
        })
                .setContentTextSize(DpUtils.dp2px(context, 8))
                .setTextColorCenter(context.getResources().getColor(R.color.color_282F3E))
                .setTextColorOut(context.getResources().getColor(R.color.color_B2B2B2))
                .build();
        pvOptions.setPicker(options1Items, options2Items, options3Items);
        pvOptions.show();
    }

}
